<?php

namespace App\Http\Controllers\Home;

use App\Model\Category;
use App\Model\FollowUser;
use App\Model\Globals;
use App\Model\Search;
use App\Models\Archive\Archive;
use App\Models\Archive\ArchiveVisit;
use App\Models\Live\Anchor;
use App\Models\Tag\Tag;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;


class searchController extends Controller
{

    public function index($key=null,$type='')
    {
        if($input = Input::except('_token','page')) {
            $key = trim($input['key']);
            $type = isset($input['type']) ? $input['type'] : '';
        }

        $recommend = '';
        if($key==null||$key=='no'){
            return $this->search();
        }

        //搜索过滤
        $key =  preg_replace('/[\x{10000}-\x{10FFFF}\s]/u', '', $key);
        $key = preg_replace('/[\?\%\*]/u', '\\\$0', $key);
        //搜索统计
        Search::record($key,Input::ip());

        //防止阿里云扫描
        if (preg_match('/images|static|js|install|script|forget|global|misc|xin|face|about/', $key)) return abort(404);

        if ($type == 'article') {
            $count = Archive::where('is_del',0)->where(function($query){
                $query->where('archive_type_id',1)
                      ->orWhere('archive_type_id',4);
            })
                ->where('title', 'like', "%" . $key . "%")
                ->ofPattern('review')
                ->count();
            $archives = Archive::where('is_del',0)->where(function($query){
                $query->where('archive_type_id',1)
                    ->orWhere('archive_type_id',4);
            })
                ->where('title', 'like', "%" . $key . "%")
                ->ofPattern('review')
                ->orderBy('updated_at', 'desc')
                ->paginate(15);
            //推荐直播
            $recommend = Anchor::where('status', 1)
                ->orderBy('online', 'desc')
                ->paginate(6);
        } elseif ($type == 'gallery') {
            $count = Archive::where('is_del',0)->where('archive_type_id', 2)
                ->where('title', 'like', "%" . $key . "%")
                ->ofPattern('review')
                ->count();
            $archives = Archive::where('is_del',0)->where('archive_type_id', 2)
                ->where('title', 'like', "%" . $key . "%")
                ->ofPattern('review')
                ->orderBy('updated_at', 'desc')
                ->paginate(6);
        } elseif ($type == 'video') {
            $count = Archive::where('is_del',0)->where('archive_type_id', 3)
                ->where('title', 'like', "%" . $key . "%")
                ->ofPattern('review')
                ->count();
            $archives = Archive::where('is_del',0)->where('archive_type_id', 3)
                ->where('title', 'like', "%" . $key . "%")
                ->ofPattern('review')
                ->orderBy('updated_at', 'desc')
                ->paginate(12);
        } else {
            $type = 'live';
            $count = Anchor::where('name', 'like', "%" . $key . "%")
                ->orwhere('room_name', 'like', "%" . $key . "%")
                ->count();
            $archives = Anchor::where('name', 'like', "%" . $key . "%")
                ->orwhere('room_name', 'like', "%" . $key . "%")
                ->orderBy('status','desc')
                ->orderBy('online', 'desc')
                ->paginate(12);
        }
        return view('home.search.' . $type, compact('count', 'type', 'recommend', 'key'))->with('archives',$archives);
    }

    public function search()
    {
        //全局关键词
        $keys = Globals::value('search_key');
        $keys = explode(',',$keys);

        return view('home.search.index')->with('keys',$keys);
    }
    public function result($key,$way='all')
    {
        $key = trim($key);
        if(empty($key)){
            return back();
        }
        //标签
        $tags = Tag::where('status',2)
            ->where('name','like',"%".$key."%")
            ->orderBy('updated_at','desc')
            ->take(10)
            ->get()
        ;

        //内容
        if($way=='video'){
            $archives = Archive::ofPattern('review')
                ->where('archive_type_id',2)
                ->where('title','like',"%".$key."%")
                ->orderBy('visit_count','desc')
                ->orderBy('updated_at','desc')
                ->paginate(10);
        }else if($way=='news'){
            $archives = Archive::ofPattern('review')
                ->where('archive_type_id',1)
                ->where('title','like',"%".$key."%")
                ->orderBy('visit_count','desc')
                ->orderBy('updated_at','desc')
                ->paginate(10);
        }else if($way=='gallery'){
            $archives = Archive::ofPattern('review')
                ->where('archive_type_id',3)
                ->where('title','like',"%".$key."%")
                ->orderBy('visit_count','desc')
                ->orderBy('updated_at','desc')
                ->paginate(10);
        }else{
            $archives = Archive::ofPattern('review')
                ->where('title','like',"%".$key."%")
                ->orderBy('visit_count','desc')
                ->orderBy('updated_at','desc')
                ->paginate(10);
        }






        return view('pc_home.search',compact('tags','archives','key','way'));
    }

}
